const db = wx.cloud.database()
// pages/search/search.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    "card": [],
    "searched": []
  },
  cardJump: function (e) {
    console.log(e)
    getApp().globalData.searched = e.currentTarget.dataset.collection
    wx.navigateTo({
      url: '../../pages/activity2/activity2'
    })
  },

  jump: function (e) {
    wx.navigateBack({
      changed: true
    });
  },

  searchGood: function (e) {
    console.log(this.data.card)
    console.log("开始", e)
    var search_key = e.detail.value.keyword
    console.log("1：", search_key)
    var searched = []
    for (var index in this.data.card) {
      console.log(this.data.card[index])

      for (var index1 in this.data.card[index]) {
        console.log("出现",this.data.card[index][index1])

        // for (var i in this.data.card[index][index1].detail) {
          // 所有商品到内容
          console.log("出来",this.data.card[index][index1].detail)
          var goods_title = this.data.card[index][index1].detail.title + this.data.card[index][index1].Category
          // console.log("这都啥啊：", this.data.card[index][index1].Category)
          if (goods_title.indexOf(search_key) >= 0) {
            var goods = this.data.card[index][index1].detail
            console.log("这都啥啊：", this.data.card[index][index1].detail)
            searched = searched.concat(goods)
          }
        // }
      }
    }
    console.log("searched=", searched)
    this.setData({
      searched: searched
    })

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    db.collection('all').get().then(res => {
      // console.log("a卡",res)
      var zhiyuan = [];
      var shetuan = [];
      var jiangzuo = [];
      var ziyou = [];
      var card = [];
      // var mid = that.data.card_mid;
      for (let i = 0; i < res.data.length; i++) {
        // console.log("空",res.data[i])
        if (res.data[i].mid == 0) {
          zhiyuan.push(res.data[i])
        };
        if (res.data[i].mid == 1) {
          shetuan.push(res.data[i])
        };
        if (res.data[i].mid == 2) {
          jiangzuo.push(res.data[i])
        };
        if (res.data[i].mid == 3) {
          ziyou.push(res.data[i])
        }
      }
      if (zhiyuan !== null) {
        card.push(zhiyuan, shetuan, jiangzuo, ziyou)
      }
      // console.log("志愿",zhiyuan)
      // console.log("社团",shetuan)
      // console.log("讲座",jiangzuo)
      // console.log("自由",ziyou)
      // console.log("全",card)
      that.setData({
        card,
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})